﻿#region

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

#endregion

namespace TTControls
{
    [ToolboxBitmap(typeof(TTBindingNavigator),"btnNext.Image")]
    public class TTBindingNavigator : ToolStrip
    {
        private Object _DataSource = null;
        private int _maxElements = 100;
        private int _minElements = 0;
        private ToolStripButton btnAdd;
        private ToolStripButton btnFirst;
        private ToolStripButton btnLast;
        private ToolStripButton btnNext;
        private ToolStripButton btnPrev;
        private ToolStripButton btnRemove;
        private ToolStripLabel lbCount;
        private ToolStripTextBox tbCurrentNumber;

        #region PublicMembers

        public Object DataSource
        {
            get { return _DataSource; }
            set
            {
                _DataSource = value;
                if (_DataSource != null)
                {
                    Form f = FindForm();
                    cm = (CurrencyManager) f.BindingContext[_DataSource];
                    UpdateState();
                }
            }
        }

        [DefaultValue(0)]
        public int MinElements
        {
            get { return _minElements; }
            set
            {
                if (value > 0 && value <= MaxElements)
                {
                    _minElements = value;
                }
                else
                {
                    throw new IndexOutOfRangeException(
                        "Wartość MinElements musi być większa od zera oraz mniejsza lub równa wartości MaxElements");
                }
            }
        }

        [DefaultValue(100)]
        public int MaxElements
        {
            get { return _maxElements; }
            set
            {
                if (value > 0 && value >= MinElements)
                {
                    _maxElements = value;
                }
                else
                {
                    throw new IndexOutOfRangeException(
                        "Wartość MaxElements musi być większa od zera oraz większa lub równa wartości MinElements");
                }
            }
        }

        public void MoveFirst()
        {
            btnFirst.PerformClick();
        }

        public void MovePrevious()
        {
            btnPrev.PerformClick();
        }

        public void MoveNext()
        {
            btnNext.PerformClick();
        }

        public void MoveLast()
        {
            btnLast.PerformClick();
        }

        public void AddNew()
        {
            btnAdd.PerformClick();
        }

        public void Remove()
        {
            btnRemove.PerformClick();
        }

        #endregion

        #region Events

        public event EventHandler ElementAdded;
        public event EventHandler<CancelEventArgs> ElementAdding;
        public event EventHandler ElementRemoved;

        #endregion

        #region PrivateMembers

        private CurrencyManager cm;

        private void InitializeComponent()
        {
            btnFirst = new ToolStripButton();
            btnPrev = new ToolStripButton();
            tbCurrentNumber = new ToolStripTextBox();
            lbCount = new ToolStripLabel();
            btnNext = new ToolStripButton();
            btnLast = new ToolStripButton();
            btnAdd = new ToolStripButton();
            btnRemove = new ToolStripButton();
            ComponentResourceManager resources = new ComponentResourceManager(typeof (TTBindingNavigator));
            SuspendLayout();

            // 
            // this
            // 
            Dock = DockStyle.None;
            Items.AddRange(new ToolStripItem[]
                               {
                                   btnFirst,
                                   btnPrev,
                                   tbCurrentNumber,
                                   lbCount,
                                   btnNext,
                                   btnLast,
                                   btnAdd,
                                   btnRemove
                               });
            Size = new Size(237, 25);

            // 
            // btnFirst
            // 
            btnFirst.DisplayStyle = ToolStripItemDisplayStyle.Image;
            btnFirst.Image = ((Image) (resources.GetObject("btnFirst.Image")));
            btnFirst.Name = "btnFirst";
            btnFirst.RightToLeftAutoMirrorImage = true;
            btnFirst.Size = new Size(23, 22);
            btnFirst.Text = "Przejdź do pierwszego";
            btnFirst.Click += new EventHandler(btnFirst_Click);
            // 
            // btnPrev
            // 
            btnPrev.DisplayStyle = ToolStripItemDisplayStyle.Image;
            btnPrev.Image = ((Image) (resources.GetObject("btnPrev.Image")));
            btnPrev.Name = "btnPrev";
            btnPrev.RightToLeftAutoMirrorImage = true;
            btnPrev.Size = new Size(23, 22);
            btnPrev.Text = "Przejdź do poprzedniego";
            btnPrev.Click += new EventHandler(btnPrev_Click);
            // 
            // tbCurrentNumber
            // 
            tbCurrentNumber.AccessibleName = "Position";
            tbCurrentNumber.AutoSize = false;
            tbCurrentNumber.Name = "tbCurrentNumber";
            tbCurrentNumber.Size = new Size(50, 23);
            tbCurrentNumber.Text = "0";
            tbCurrentNumber.ToolTipText = "Aktualna pozycja";
            tbCurrentNumber.ReadOnly = true;
            // 
            // lbCount
            // 
            lbCount.Name = "lbCount";
            lbCount.Size = new Size(35, 22);
            lbCount.Text = "z {0}";
            lbCount.ToolTipText = "Ilość pozycji";
            // 
            // btnNext
            // 
            btnNext.DisplayStyle = ToolStripItemDisplayStyle.Image;
            btnNext.Image = ((Image) (resources.GetObject("btnNext.Image")));
            btnNext.Name = "btnNext";
            btnNext.RightToLeftAutoMirrorImage = true;
            btnNext.Size = new Size(23, 22);
            btnNext.Text = "Przejdź do następnego";
            btnNext.Click += new EventHandler(btnNext_Click);
            // 
            // btnLast
            // 
            btnLast.DisplayStyle = ToolStripItemDisplayStyle.Image;
            btnLast.Image = ((Image) (resources.GetObject("btnLast.Image")));
            btnLast.Name = "btnLast";
            btnLast.RightToLeftAutoMirrorImage = true;
            btnLast.Size = new Size(23, 22);
            btnLast.Text = "Przejdź do ostatniego";
            btnLast.Click += new EventHandler(btnLast_Click);
            // 
            // btnAdd
            // 
            btnAdd.DisplayStyle = ToolStripItemDisplayStyle.Image;
            btnAdd.Image = ((Image) (resources.GetObject("btnAdd.Image")));
            btnAdd.Name = "btnAdd";
            btnAdd.RightToLeftAutoMirrorImage = true;
            btnAdd.Size = new Size(23, 22);
            btnAdd.Text = "Dodaj";
            btnAdd.Click += new EventHandler(btnAdd_Click);
            // 
            // btnRemove
            // 
            btnRemove.DisplayStyle = ToolStripItemDisplayStyle.Image;
            btnRemove.Image = ((Image) (resources.GetObject("btnRemove.Image")));
            btnRemove.Name = "btnRemove";
            btnRemove.RightToLeftAutoMirrorImage = true;
            btnRemove.Size = new Size(23, 22);
            btnRemove.Text = "Usuń";
            btnRemove.Click += new EventHandler(btnRemove_Click);

            ResumeLayout(false);
            PerformLayout();
        }

        private void UpdateState()
        {
            lbCount.Text = "z " + cm.Count.ToString();
            if (cm.Count <= MinElements)
            {
                btnRemove.Enabled = false;
            }
            else
            {
                btnRemove.Enabled = true;
            }

            if (cm.Count >= MaxElements)
            {
                btnAdd.Enabled = false;
            }
            else
            {
                btnAdd.Enabled = true;
            }
            tbCurrentNumber.Text = (cm.Position + 1).ToString();

            if (cm.Position <= 0) //tak, może być -1
            {
                btnPrev.Enabled = false;
                btnFirst.Enabled = false;
            }
            else
            {
                btnPrev.Enabled = true;
                btnFirst.Enabled = true;
            }
            if (cm.Position == cm.Count - 1)
            {
                btnNext.Enabled = false;
                btnLast.Enabled = false;
            }
            else
            {
                btnNext.Enabled = true;
                btnLast.Enabled = true;
            }
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (cm.Position > 0)
            {
                cm.Position--;
            }
            UpdateState();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            cm.Position = 0;
            UpdateState();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            cm.Position = cm.Count - 1;
            UpdateState();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (ElementAdding != null)
            {
                CancelEventArgs cea = new CancelEventArgs();
                ElementAdding(this, cea);
                if (!cea.Cancel)
                {
                    cm.AddNew();
                    if (ElementAdded != null)
                    {
                        ElementAdded(this, EventArgs.Empty);
                    }
                    UpdateState();
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            cm.RemoveAt(cm.Position);
            if (ElementRemoved != null)
            {
                ElementRemoved(this, EventArgs.Empty);
            }
            UpdateState();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (cm.Position < cm.Count - 1)
            {
                cm.Position++;
            }
            UpdateState();
        }

        #endregion

        public TTBindingNavigator()
        {
            InitializeComponent();
        }
    }
}